//
//  LoginViewController.m
//  QQ-Music
//
//  Created by lanou3g on 15/10/30.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#import "RegistViewController.h"
#import <BmobSDK/Bmob.h>
#import "User.h"

    

@interface LoginViewController ()<UserDelegate>

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, 20, 18);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
 
}


#pragma mark - 导航栏Button点击事件
- (void) openOrCloseLeftList
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed)
    {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
}
#pragma mark - 注册按钮
- (IBAction)registButton:(id)sender {
    RegistViewController *VC = [[RegistViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
    
}

#pragma mark - 登录的点击方法
- (IBAction)loginButtonDidClicked:(id)sender {
    NSString * username = [_usernameTextField text];
    NSString * password = [_passwordTextField text];
    [[User sharedInstance] loginWithUserName:username withPassword:password];
    [User sharedInstance].delegate = self;
}

#pragma mark - delegate
-(void)judgeUserLoginStateWithUser:(User *)user withIsLogin:(BOOL)isLogin
{
//    if (isLogin) {//如果有该用户
//        [user userEmailVerified:^(BOOL isSuccessful, NSError *error) {
//            if (isSuccessful) { //如果该用户的邮箱已经激活
//                [self alterWithMessage:@"登录成功"];
//                [self performSelector:@selector(removeAlterController:) withObject:self afterDelay:3];
//            } else {
//                [self alterWithMessage:@"登录失败，请确保邮箱已经激活"];
//            }
//            
//        }];
//        
//    }else {
//        [self alterWithMessage:@"登录失败，用户不存在！"];
//    }
    
    
    [BmobUser loginInbackgroundWithAccount:self.usernameTextField.text andPassword:self.passwordTextField.text block:^(BmobUser *user, NSError *error) {
        
        if (user) {//如果有该用户
            [user userEmailVerified:^(BOOL isSuccessful, NSError *error) {
                if (isSuccessful) { //如果该用户的邮箱已经激活
                    NSLog(@"登录成功");
                } else {
                    NSLog(@"邮箱可能没有激活---%@",error);
                }
                
            }];
            
        }else {
            NSLog(@"用户不存在：%@",error);
        }
        
    }];

    
}

-(void)removeAlterController:(UIAlertController *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 弹框
-(void)alterWithMessage:(NSString *)message{
    UIAlertController *alterController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        
    }];
    [alterController addAction:cancleAction];
    [alterController addAction:confirmAction];
    [self.navigationController presentViewController:alterController animated:YES completion:nil];
}
- (IBAction)sinaDidClicked:(id)sender {
//    //得到的新浪微博授权信息，请按照例子来生成NSDictionary
//    NSDictionary *dic = @{@"access_token":token,@"uid":uid,@"expirationDate":date};
//    //通过授权信息注册登录
//    [BmobUser loginInBackgroundWithAuthorDictionary:dic
//                                           platform:BmobSNSPlatformSinaWeibo
//                                              block:^(BmobUser *user, NSError *error) {
//                                                  NSLog(@"user objectid is :%@",user.objectId);
//                                              }];
}


@end
